/*$(document).ready(function () {
    App.init();
    TableManageFixedHeader.init();

    $('.datepicker-default').datepicker({
        language: 'zh-CN',//显示中文
        format: 'yyyy-mm-dd',//显示格式
        minView: "month",//设置只显示到月份
        initialDate: new Date(),//初始化当前日期
        autoclose: true,//选中自动关闭
        todayBtn: 'linked'//显示今日按钮
    });

});*/
$(document).ready(function() {

    App.init();
    TableManageFixedHeader.init();
    wrapTable1();
    wrapTable2();

    $('.selectpicker').selectpicker({
        style: 'btn-white',
        size: 5
    });
    //日期控件
    $(".datepicker-default").datepicker({
        autoclose: true, //自动关闭
        beforeShowDay: $.noop,    //在显示日期之前调用的函数
        calendarWeeks: false,     //是否显示今年是第几周
        clearBtn: false,          //显示清除按钮
        daysOfWeekDisabled: [],   //星期几不可选
        endDate: Infinity,        //日历结束日期
        forceParse: true,         //是否强制转换不符合格式的字符串
        format: 'yyyy-mm-dd',     //日期格式
        keyboardNavigation: true, //是否显示箭头导航
        language: "zh-CN",           //语言
        minViewMode: 0,
        orientation: "auto",      //方向
        rtl: false,
        startDate: -Infinity,     //日历开始日期
        startView: 0,             //开始显示
        todayBtn: false,          //今天按钮
        todayHighlight: true,    //今天高亮
        weekStart: 0
    });


    //业务员赋值
    $("#email").click(function () {
        var value = $("#email :selected").val();
        if(value!="请选择"){
            $('#userName').val($('#email :selected').text());
        }
    });

    //保险公司赋值
    $("#select").click(function () {
        var value = $('#select :selected').val();
        if(value!="请选择"){
            $('#company').val($('#select :selected').text());
        }
    })



});


//分页展示
var table1;
var table2;
function wrapTable1() {
    table1 = $("#modeltable").DataTable({
        "bAutoWidth": false,
        /*"order": [[0, 'desc']],*/
        ordering:false,
        "pagingType": "simple_numbers",//设置分页控件的模式
        searching: false,//屏蔽datatales的查询框
        aLengthMenu: [15],//设置一页展示10条记录
        "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
        "oLanguage": {  //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "没有找到符合条件的数据",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "没有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true,//打开后台分页
        "ajax": {
            "url": "/ins/order/getInsyrerData",
            "dataSrc": "aaData",
            "data": function (params) {
                var insyrerId  =$('#insyrerId :selected').val();
                if(insyrerId==0){
                    params.insyrerId =null;
                }else{
                    params.insyrerId = insyrerId;
                }
                var channelId =  $('#channelId :selected').val();
                params.ordNum = $('#ordNum').val();
                params.plateNum = $('#plateNum').val();
                if (channelId==0){
                    params.channelId =null;
                }else{
                    params.channelId =channelId;
                }
                params.insureStartDate = $('#insureStartDate').val();
                params.insureEndDate = $('#insureEndDate').val();
            }
        },
        "columns": [
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var tids = row.ordNum;
                    return "<a href='javascript:void(0);' onclick='toDetails("+tids+")'>"+tids+"</a>";
                }
            },
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var paySubject = row.paySubject;
                    if(paySubject==1){
                        return "环宇康泰";
                    }else if(paySubject==2){
                        return "青岛盛达";
                    }else if(paySubject==3){
                        return "玉环天云";
                    }else {
                        return "汉龙";
                    }
                }
            },
            {
                "data": "insyrerName",
                "bSortable": false
            },
            {
                "data": "channel.companyRegisterName",
                "bSortable": false
            },
            {
                "data": "channel.contact.telephone",
                "bSortable": false
            },
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var typeInsurance = row.typeInsurance;
                    if(typeInsurance==0){
                        return "交强险";
                    }else if(typeInsurance==1){
                        return "商业险";
                    }
                }
            },
            {
                "data": "insurant",
                "bSortable":false
            },
            {
                "data": "orgIdCode",
                "bSortable":false
            },
            {
                "data": "plateNum",
                "bSortable":false
            },
            {
                "data": "carOwner",
                "bSortable":false
            },
            {
                "data": "mercialPremium",
                "bSortable":false
            },
            {
                "data": "pulsoryPremium",
                "bSortable":false
            },
            {
                "data": "vechcleVesselTax",
                "bSortable":false
            },
            {
                "data": "allPremium",
                "bSortable":false
            },
            {
                "data": "mustRebateAmt",
                "bSortable":false
            },
            {
                "data": "dateIssuance",
                "bSortable":false
            },
            {
                "data": "insureStartDate",
                "bSortable":false
            },
            {
                "data": "receiptDate",
                "bSortable":false
            },
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var checkResult = row.checkResult;
                    if(checkResult==1){
                        return "一致";
                    }else if(checkResult==2){
                        return "不一致";
                    }
                }
            },
            {
                "data": "realRebateAmt",
                "bSortable":false
            },
        ],

        "columnDefs": [{
            "targets": 20,//是操作按钮目标列
            "data": null,
            "render": function (data, type, row) {
                var status = row.status;
                if(status==1){
                    var ordId = "'"+row.tid+"'"+","+row.capitalId;
                    var capitalId = "'"+row.capitalId+"'";
                    // return '<a href="javascript:void(0);" onclick="openModels(\''+ids+'\')">'+ids+'</a>';
                    return '<a href="javascript:void(0);" onclick="openModels('+ordId+')">退保</a>';
                }else{
                    return null;
                }
            }
        }]
    });

}

function wrapTable2() {
    table2 = $("#modeltable1").DataTable({
        "bAutoWidth": false,
        /*"order": [[0, 'desc']],*/
        ordering:false,
        "pagingType": "simple_numbers",//设置分页控件的模式
        searching: false,//屏蔽datatales的查询框
        aLengthMenu: [15],//设置一页展示10条记录
        "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
        "oLanguage": {  //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "没有找到符合条件的数据",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "没有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true,//打开后台分页
        "ajax": {
            "url": "/ins/order/getChannelData",
            "dataSrc": "aaData",
            "data": function (params) {
                var insyrerId  =$('#insyrerId1 :selected').val();
                if(insyrerId==0){
                    params.insyrerId =null;
                }else{
                    params.insyrerId = insyrerId;
                }
                var channelId =  $('#channelId1 :selected').val();
                if (channelId==0){
                    params.channelId =null;
                }else{
                    params.channelId =channelId;
                }
                params.ordNum = $('#ordNum1').val();
                params.plateNum = $('#plateNum1').val();
                params.insureStartDate = $('#insureStartDate1').val();
                params.insureEndDate = $('#insureEndDate1').val();
            }
        },
        "columns": [
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var tid = row.ordNum;
                    return "<a href='javascript:void(0);' onclick='toDetails("+tid+")'>"+tid+"</a>";
                }
            },
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var paySubject = row.paySubject;
                    if(paySubject==1){
                        return "环宇康泰";
                    }else if(paySubject==2){
                        return "青岛盛达";
                    }else if(paySubject==3){
                        return "玉环天云";
                    }else {
                        return "汉龙";
                    }
                }

            },
            {
                "data": "insyrerName",
                "bSortable": false
            },
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    if(row.channel.companyRegisterName==null){
                        return row.channel.contact.name;
                    }else{
                        return row.channel.companyRegisterName;
                    }
                }
            },
            {
                "data": "channel.contact.telephone",
                "bSortable": false
            },
            {
                "bSortable": false,
                "render": function (data, type, row, meta) {
                    var typeInsurance = row.typeInsurance;
                    if(typeInsurance==0){
                        return "交强险";
                    }else if(typeInsurance==1){
                        return "商业险";
                    }
                }
            },
            {
                "data": "insurant",
                "bSortable":false
            },
            {
                "data": "orgIdCode",
                "bSortable":false
            },
            {
                "data": "plateNum",
                "bSortable":false
            },
            {
                "data": "mercialPremium",
                "bSortable":false
            },
            {
                "data": "pulsoryPremium",
                "bSortable":false
            },
            {
                "data": "vechcleVesselTax",
                "bSortable":false
            },
            {
                "data": "allPremium",
                "bSortable":false
            },
            {
                "data": "dateIssuance",
                "bSortable":false
            },
            {
                "data": "insureStartDate",
                "bSortable":false
            },
            {
                "data": "channelRebate",
                "bSortable":false
            },
            {
                "data": "mustRebateAmt",
                "bSortable":false
            },
            {
                "data": "payDate",
                "bSortable":false
            },
            {
                "data": "mustPremium",
                "bSortable":false
            },
            {
                "data": "insyrerMaturityDate",
                "bSortable":false
            }
        ]
    });

}


//详情页
function toDetails(tid) {
}

//查询
function search() {
    table1.ajax.reload();
}
function search1() {
    table2.ajax.reload();
}
//打开模态框
function openModels(ordId,capitalId) {
    /*console.log("ordId"+ordId)
    var ordIds = ordId.split(",")[0];
    var capitalId = ordId.split(",")[1];*/
    console.log("ordId"+ordId)
    console.log("capitalId"+capitalId)

    surrender(capitalId,ordId);
}

function doSomeThing() {
    var type = $("#typee").val();
    if(type==1){
        exportTables();
    }else if(type==2){
        exportChannelTable();
    }else if(type==4){
        $('#modalSurrender').modal('hide');
    }else{
        table1.ajax.reload();
        table2.ajax.reload();
    }
    $('#modal-dialog').modal('hide')
}
//下载
function exportTables() {
    var insyrerId = $('#insyrerId :selected').val()==0?null:$('#insyrerId :selected').val();
    var ordNum = $('#ordNum').val();
    var plateNum = $('#plateNum').val();
    var channelId = $('#channelId :selected').val()==0?null:$('#channelId :selected').val();
    var insureStartDate = $('#insureStartDate').val();
    var insureEndDate = $('#insureEndDate').val();
    window.location.href="/ins/order/download?insyrerId="+insyrerId+"&ordNum="+ordNum
        +"&channelId="+channelId+"&insureStartDate="+insureStartDate+"&insureEndDate="+insureEndDate
        +"&plateNum="+plateNum;
    //window.location.href = "/ins/order/download";

}

function exportChannelTable() {
    var insyrerId = $('#insyrerId1 :selected').val()==0?null:$('#insyrerId1 :selected').val();
    var ordNum = $('#ordNum1').val();
    var plateNum = $('#plateNum1').val();
    var channelId = $('#channelId1 :selected').val()==0?null:$('#channelId1 :selected').val();
    var insureStartDate = $('#insureStartDate1').val();
    var insureEndDate = $('#insureEndDate1').val();
    window.location.href="/ins/order/downloadChannel?insyrerId="+insyrerId+"&ordNum="+ordNum
        +"&channelId="+channelId+"&insureStartDate="+insureStartDate+"&insureEndDate="+insureEndDate
        +"&plateNum="+plateNum;
    //window.location.href = "/ins/order/download";

}

//应收订单退保
function surrender(capitalId, ordId) {
    var formData = new FormData();
    formData.append("capitalId", capitalId);
    formData.append("ordId", ordId);
    $.ajax({
        url: "/receivable/checkSurrenderData",
        type: "POST",
        data: formData,
        //必须false才会自动加上正确的Content-Type
        contentType: false,
        //必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理
        processData: false,
        success: function (data) {
            if (data.success == true) {
                var channelFlag = data.result.channelFlag;
                var channelStatus = data.result.channelStatus;
                var insureStartDate = data.result.insureStartDate;
                console.log("insureStartDate"+insureStartDate)
                if (channelStatus == 4) {
                    $("#me").html("请先完成该渠道应收保费的结算审核");
                    openModel(3);
                    return false;
                } else {
                    if (channelFlag == 1) {
                        $('#mustPremium').removeClass('hidden');
                        $('#inputMustPremium').attr('type','text');
                        $('#mustPremium').val('');
                    }
                    $('#surrenderTid').val(capitalId);
                    $('#orderTid').val(ordId);
                    $('#channelFlag').val(channelFlag);
                    $('#inputInsuranceDate').html(insureStartDate);
                    $('#inputSurrenderDate').val(new Date().Format("yyyy-MM-dd"));
                    $('#modalSurrender').modal({backdrop: 'static', keyboard: false});
                }
            } else {
                $("#me").html(data.msg);
                openModel(3);
                return;
            }
        },
        error: function () {
            $("#me").html(data.msg);
            openModel(3);
        }
    });
}

function orderSurrender() {
    console.log(1111111)

    var status = $('#myForm').submit().parsley();
    console.log(status.isValid())
    if(status.isValid()){

        $.ajax({
            url: "/receivable/orderSurrender",
            type: "POST",
            async:true,
            data: $('#myForm').serialize(),
            //必须false才会自动加上正确的Content-Type
            //必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理
            success: function (data) {
                if (data.success == true) {
                    $('#modalSurrender').modal('hide');
                    $("#me").html(data.msg);
                    openModel(3);
                } else {
                    $("#me").html(data.msg);
                    openModel(3);
                    return;
                }
            }
        });
    }
}
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function openModel(type) {
    $("#typee").val(type);
    if (type==1){
        $("#me").html("确认下载当前列表信息");
    }else if(type==2){
        $("#me").html("确认下载当前列表信息");

    }else if(type==4){

    }else{
        $('#modalSurrender').modal('hide')
    }
    $('#modal-dialog').modal({backdrop: 'static', keyboard: false});
}
//关闭模态框
function closeModel() {
    /*$('#modal-dialog').modal('hide');
    location=location;*/
    $("#me").html("当前填写内容未提交，确认取消？");
    openModel(4);
}
